//
//  QuestionViewController.m
//  HappyMother
//
//  Created by 易东 on 2017/10/27.
//  Copyright © 2017年 东哥. All rights reserved.
//

#import "QuestionViewController.h"
#import "ZLPhotoActionSheet.h"
@interface QuestionViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@end

@implementation QuestionViewController
{
    UIScrollView *showScrollView;
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self createNaviBarButton];
    self.title = @"提问";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self createUI];
}
#pragma mark - 创建UI
-(void)createUI{
    
    showScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, self.view.height)];
    showScrollView.showsVerticalScrollIndicator = 0;
    [self.view addSubview:showScrollView];
    
    UIButton *btnt = [[UIButton alloc] initWithFrame:CGRectMake(40, 250, Screen_Width-80, 40)];
    [btnt setBackgroundColor:COLOR];
    [showScrollView addSubview:btnt];
    [btnt setTitle:@"上传相片" forState:UIControlStateNormal];
    [btnt addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    
}


-(void)click:(UIButton *)btn{
    

    NSString *picButtonTitle = NSLocalizedString(@"从相册选择", UIColoerFromRGB(0x999999));
    
    NSString *destructiveButtonTitle = NSLocalizedString(@"拍照", nil);
    
    NSString *cancel = NSLocalizedString(@"取消", nil);
    
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancel style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
        NSLog(@"取消");
        
    }];
    
    
    UIAlertAction *piclAction = [UIAlertAction actionWithTitle:picButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        NSLog(@"相册");
        ZLPhotoActionSheet *actionSheet = [self getPhotoActionSheet];
        [actionSheet showPhotoLibrary];
        
    }];
    
    
#pragma mark - 拍照
    UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:destructiveButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        NSLog(@"照相");
        
        if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            //资源类型为照相机
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:nil];
        }else {
            NSLog(@"该设备无摄像头");
        }
        
    }];
    
    
    [alertController addAction:piclAction];
    
    [alertController addAction:destructiveAction];
    
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];

}

#pragma mark - 获取图片
- (ZLPhotoActionSheet *)getPhotoActionSheet
{
    ZLPhotoActionSheet *actionSheet = [[ZLPhotoActionSheet alloc] init];
    actionSheet.navBarColor = [UIColor lightGrayColor];
    //设置照片最大预览数
    actionSheet.maxPreviewCount = 20;
    //设置照片最大选择数
    actionSheet.maxSelectCount = 9;
    actionSheet.sender = self;
    actionSheet.showSelectBtn = YES;
    actionSheet.showSelectedMask = 0;
    actionSheet.allowSelectVideo = 0;
    actionSheet.allowSelectLivePhoto = 0;
    
    
#pragma required
    //如果调用的方法没有传sender，则该属性必须提前赋值
    actionSheet.sender = self;
    
    [actionSheet setSelectImageBlock:^(NSArray<UIImage *> * _Nonnull images, NSArray<PHAsset *> * _Nonnull assets, BOOL isOriginal) {
        
        NSLog(@"images====%@",images);
        
    }];
    
    return actionSheet;
}
#pragma UIImagePickerController Delegate 获取图片上传服务器
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    NSLog(@"拍照。。。。image====%@",image);
    
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}
@end
